<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="name" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model="age" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="sex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="btn" v-show="ishow">添加/修改</button>
      <button @click="xiugai(num)" v-show="!ishow">添加/修改2</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, index) in list" :key="index">
          <td>{{ index }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.sex }}</td>

          <td>
            <button @click="del(index)">删除</button>
            <button @click="bianji(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ishow: true,
      name: '',
      age: '',
      sex: '',
      list: [],
      num:0

    };
  },
  methods: {
    btn() {
      const arr = {};
      (arr.name = this.name), (arr.age = this.age), (arr.sex = this.sex);
      this.list.push(arr);
      this.name = '';
      this.age = '';
      this.sex = '';
    },
 
    del(index) {
      this.list.splice(index, 1);
    },
    bianji(index) {
      this.name = this.list[index].name;
      this.age = this.list[index].age;
      this.sex = this.list[index].sex;

  this.num=index
      this.ishow=!this.ishow
    },
    xiugai() {
     
     this.list[this.num].name=this.name
     this.list[this.num].age=this.age
     this.list[this.num].sex=this.sex
     this.name ='',
     this.age='',
     this.sex=''

     this.ishow=!this.ishow
        window.console.log(this.name)
       },

  },
};
</script>
